﻿using System;
using System.Windows.Forms;
using System.Collections.Generic;

//自定义命名空间
using DreamCube.Apps.Common.WinForm.Controls;

namespace WebServer.UI
{
    public partial class ServerEditForm : Form
    {
        public ServerEditForm()
        {
            InitializeComponent();
        }

        #region "私有方法"

        /// <summary>
        /// 添加按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            WebBindingEditForm form = new WebBindingEditForm();
            if (form.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                Service.Model.WebBinding binding = form.WebBinding;
                dataGridView1.Rows.Add(new Object[] { binding.Protocol, binding.Port, binding.Hostname });
            }
        }

        /// <summary>
        /// 编辑按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow == null) return;
            Service.Model.WebBinding binding = new Service.Model.WebBinding()
            {
                Protocol = dataGridView1.CurrentRow.Cells[0].Value.ToString(),
                Port = Convert.ToInt32(dataGridView1.CurrentRow.Cells[1].Value),
                Hostname = dataGridView1.CurrentRow.Cells[2].Value.ToString()
            };
            WebBindingEditForm form = new WebBindingEditForm(binding);
            if (form.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                dataGridView1.CurrentRow.Cells[0].Value = form.WebBinding.Protocol;
                dataGridView1.CurrentRow.Cells[1].Value = form.WebBinding.Port;
                dataGridView1.CurrentRow.Cells[2].Value = form.WebBinding.Hostname;
            }
        }

        /// <summary>
        /// 删除按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows != null && dataGridView1.SelectedRows.Count > 0)
            {
                if (MessageBoxHelper.YesNo(this, "确定要删除选中记录？") == System.Windows.Forms.DialogResult.Yes)
                {
                    foreach (DataGridViewRow row in dataGridView1.SelectedRows)
                        dataGridView1.Rows.Remove(row);
                }
            }
        }

        /// <summary>
        /// 启动服务器按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count == 0)
                MessageBoxHelper.Information(this, "请配置服务器绑定记录");
            List<String> prefixList = new List<String>();
            for (Int32 i = 0; i < dataGridView1.Rows.Count; i++)
            {
                Service.Model.WebBinding binding = new Service.Model.WebBinding()
                {
                    Protocol = dataGridView1.Rows[i].Cells[0].Value.ToString(),
                    Port = Convert.ToInt32(dataGridView1.Rows[i].Cells[1].Value),
                    Hostname = dataGridView1.Rows[i].Cells[2].Value.ToString()
                };
                String prefix = Service.BLL.Utility.WebBindingToPrefix(binding);
                prefixList.Add(prefix);
            }
            Service.BLL.WebServer server =
                new Service.BLL.WebServer(textBox1.Text, prefixList.ToArray());
            server.Start();
        }

        /// <summary>
        /// 窗体加载时触发此事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_Load(object sender, EventArgs e)
        {
            //从数据库中加载记录
            Service.DAL.WebBinding bindingTable = new Service.DAL.WebBinding();
            List<Service.Model.WebBinding> bindingObjs = bindingTable.GetModelListEx();
            if(bindingObjs!=null && bindingObjs.Count>0)
            {
                for (Int32 i = 0, j = bindingObjs.Count; i < j; i++)
                {
                    Service.Model.WebBinding tempBinding = bindingObjs[i];
                    dataGridView1.Rows.Add(new Object[] { tempBinding.Protocol, tempBinding.Port, tempBinding.Hostname });
                }
            }
        }

        /// <summary>
        /// 浏览网站目录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                textBox1.Text = folderBrowserDialog1.SelectedPath;
        }

        #endregion
    }
}
